#include "dropdownbutton.h"
#include "keycontrollablemenu.h"

DropDownButton::DropDownButton(QIcon icon, QString name,QWidget * parent) : Button(icon,name,parent)
{
    menu=new PopupMenu(parent);
    menu->hide();
    //menu->setMinimumSize(QSize(200,200));
    connect(menu,SIGNAL(selected(Button*)),this,SLOT(buttonSelected(Button*)));
}

void DropDownButton::addButton(Button *button) {
    menu->addButton(button);
}

void DropDownButton::activate(QWidget * parentW) {
    menu->grabKeyboard();
    QWidget * parentWidget= qobject_cast<QWidget *>(menu->parent());
    QWidget * thisParent= qobject_cast<QWidget *>(parent());
    qDebug()<<"parent's dimensions "<<parentWidget->width()<<" "<<parentWidget->height();
    qDebug()<<"parent's dimensions "<<thisParent->width()<<" "<<thisParent->height();
    KeyControllableMenu::displayPopup(menu,parentWidget,thisParent->frameGeometry().x()+frameGeometry().x(),thisParent->frameGeometry().y()+parentWidget->frameGeometry().y()+frameGeometry().y()+height()+60);
}

void DropDownButton::buttonSelected(Button * button) {
    setText(button->text());
    emit selected(button);
}

bool DropDownButton::hasChildren() {
    return (menu->children().count()>0);
}

void DropDownButton::deactivate() {
    menu->hide();
}
